﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json;
using Group3.MediaTool.Interface;
using Group3.MediaTool.OWIN.Model;
using Group3.MediaTool.Generator;
using System.Xml.Linq;
using Group3.MediaTool.Core;
using Group3.MediaTool.Data.IO;
using Group3.MediaTool.Data.Generator;

namespace Group3.MediaTool.OWIN.Controller
{
    public class WebDefinition
    {
        public string Name { get; set; }
        public string PType { get; set; }
    }


    /// <summary>
    /// GenerateCategoryController handles the Actions for generating Classes.
    /// This Actions must be specified as standard functions for Class- and DataStructure generating
    /// </summary>
    public class GenrateCategoryController : ApiController
    {
        [HttpPost]
        public void Post(int action_id, string jsonString = "")
        {
            try
            {
                IObjectWebDefinition data = (ObjectWebDefinition)JsonConvert.DeserializeObject(jsonString, typeof(ObjectWebDefinition));

                XDocument newClass = new XDocument();
                newClass.Add(new XElement("class", new XAttribute("id", data.OrmName)));
                newClass.Element("class")
                    .Add(new XElement("name", data.OrmName));

                newClass.Element("class")
                    .Add(new XElement("change", "create")); //getFrom data!!!

                newClass.Element("class")
                    .Add(new XElement("BackEndDef", ""));


                //["
                //{\"Name\":\"test22\",\"PType\":\"String\",\"Counter\":0}",
                //"{\"Name\":\"New Property name\",\"PType\":\"String\",\"Counter\":1}
                //"]
                string[] stringArray = (string[])JsonConvert.DeserializeObject(data.Definitions, typeof(string[]));
                foreach (var itm in stringArray)
                {
                    WebDefinition def = (WebDefinition)JsonConvert.DeserializeObject(itm, typeof(WebDefinition));

                    string isKey = "";
                    string isIndex = "";
                    string isNullable = "";

                    string defPType = ""; 

                    //Get all DataType definitions of the frontend!!! TODO !!!!!
                    switch(def.PType.ToLower())
                    {
                        case "string":
                              defPType = "nVarchar(MAX)"; break;
                        case "number":
                            defPType = "int"; break;

                        default:
                              defPType = "nVarchar(255)"; break;
                    }
                    //---TODO !!!

                    XElement newAttribute = new XElement("attribute");
                    newAttribute.Add(new XElement("change"));
                    newAttribute.Add(new XElement("name", def.Name));
                    newAttribute.Add(new XElement("type", defPType));
                    newAttribute.Add(new XElement("key", isKey));
                    newAttribute.Add(new XElement("index", isIndex));
                    newAttribute.Add(new XElement("nullable", isNullable));
                    newClass.Element("class").Add(newAttribute);
                }

                //TODO!!! //
                StaticConfig.addClassToConfig(newClass);
                DataDefinitionClass ddc = new DataDefinitionClass();
                ddc.createObjects();

                IObjectDefinition objDef = data.ToIObjectDefinition();
                ClassBuilder cb = new ClassBuilder();
                cb.BuildClass(objDef);
            }
            catch (Exception)
            {
                //TODO: Sauberes Fehlerhandling
            }
        }
    }
}
